<template>
  <div class="login-wrap">
    <el-form
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <h3 class="title">用户注册</h3>
      <el-form-item prop="uname">
        <el-input
          type="text"
          v-model="ruleForm.uname"
          auto-complete="off"
          placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input
          type="text"
          v-model="ruleForm.phone"
          auto-complete="off"
          placeholder="手机"
        ></el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input
          type="text"
          v-model="ruleForm.email"
          auto-complete="off"
          placeholder="邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          type="password"
          v-model="ruleForm.pwd"
          auto-complete="off"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="entryDate" style="width: 100%">
        <el-col :span="11">
          <el-date-picker
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="入职时间"
            v-model="ruleForm.entryDate"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item prop="code">
        <el-input
          v-model="ruleForm.code"
          style="width: 50%"
          placeholder="邮箱验证码"
        ></el-input>
        <el-button
          type="primary"
          @click="sendEmailCode()"
          v-bind:disabled="buttonDisabled"
          >发送验证码</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 100%"
          @click="submitForm('ruleForm')"
          >注册账号</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 100%"
          @click="tologin()"
          >返回登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script type="text/ecmascript-6">
import { register, sendCode } from "../api/basisMG";
import {
  validatePhone,
  validatePassword,
  validateUsername,
  validateEmail,
} from "../utils/util";
export default {
  name: "sign",
  data() {
    return {
      issend: true,
      buttonDisabled: false,
      ruleForm: {
        uname: "",
        sex: "男",
        phone: "",
        email: "",
        pwd: "",
        entryDate: "",
        code: "",
      },
      params: {
        email: "",
      },
      //rules前端验证
      rules: {
        uname: [
          { required: true, validator: validateUsername, trigger: "blur" },
        ],
        email: [{ required: true, validator: validateEmail, trigger: "blur" }],
        pwd: [{ required: true, validator: validatePassword, trigger: "blur" }],
        phone: [{ required: true, validator: validatePhone, trigger: "blur" }],
        entryDate: [
          { required: true, message: "请输入入职时间", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入邮箱验证码", trigger: "blur" },
        ],
      },
    };
  },
  // 创建完毕状态(里面是操作)
  created() {},
  // 里面的函数只有调用才会执行
  methods: {
    // 发送邮箱验证码并验证账号
    sendEmailCode() {
      this.params.email = this.ruleForm.email;
      sendCode(this.params).then((res) => {
        if (res.status == 200) {
          this.$message.success("验证码已发送");
          // 禁用按钮
          this.buttonDisabled = true;
        } else {
          this.$message.error("邮箱已注册");
        }
      });
    },
    // 注册账号
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register(this.ruleForm).then((res) => {
            if (res.status == 200) {
              //如果请求成功就让他2秒跳转路由
              setTimeout(() => {
                this.$message.success("账号注册成功");
                this.$router.push({ path: "/login" });
              }, 1000);
            } else {
              this.$message.error(res.msg);
              return false;
            }
          });
        } else {
          this.$message.error("请输入必填信息！");
          this.logining = false;
          return false;
        }
      });
    },
    tologin() {
      this.$router.push({ path: "/login" });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
</style>